﻿$(function () {

    var $wallBackground = $('.background'), $joinBackground = $('#joinBackground'),
        wallHeight = 0;
    //初始化墙板
    var sizingWall = function () {
        var _dh = document.documentElement.clientHeight;
        wallHeight = _dh - 90;
        $wallBackground.css('height', wallHeight + 'px');
    };
    sizingWall();
    $(window).resize(function () {
        sizingWall();
    });

    //初始化新人信息
    var PATH_INFO = '/wall/getinfo?callback=?';
    var id = getQueryString('id');

    var $man = $('#man'), $woman = $('#woman'), $img = $('#headImg');

    var getCoupleInfo = function () {
        $.get(PATH_INFO, {
            'id': id
        }, function (res) {
            $man.html(res.groom_name);
            $woman.html(res.bride_name);
            $img.attr('src', res.image);
            timeLoad();
            //setInterval(timeLoad, 2000);
        }, 'json').error(function () {
            timeLoad();
        });
    };
   getCoupleInfo();

    var PATH_BLESS = 'http://www.hdzhx.com/zxims/f/wechat/screen/weddingScreen/toScreen?srnid=1';
    var $bless = $('#blesses'), $join = $('#joinUsers'), time = Date.now() / 1000, counts = 0, rollCount = 0;
    id = '1';
    //定时获取新的POST消息
    var timeLoad = function () {
        time = Date.now() / 1000;
        $.get(PATH_BLESS,  function (res) {
        	////alert("the result :"+res.data.list[0].content);
        	////
        	 if ($.isPlainObject(res) && res["ret"] == 0) {
                res = res["data"];
                var list = res.list;// user = res.user;
                $.each(list, function (i, row) {
                    var wxid = row.wxid, content = row.content, time = row.send_time / 1000, name = '', photo = '';
                    
                    name = row.name;
                    photo = ROOT + "/screen/wedding-default/images/default.jpg";
                    if (!document.getElementById(wxid)) {
                        var urr = [];
                        urr.push('<li class="join-user" id="' + wxid + '" uname="' + name + '">');
                        urr.push('  <img src="' + photo + '" onerror=\"this.src="/static/screen/wedding-default/images/default.jpg"\" />');
                       // urr.push('  <div class="name">' + name + '</div>');
                        urr.push('</li>');
                        $join.append(urr.join(''));
                        counts++;
                        $join.css('width', 290 * counts + 'px');
                    }

                    var arr = [];
                    arr.push('<div class="bless clearfix">');
                    arr.push('  <div class="mc"></div>');
                    arr.push('  <div class="head"><img src="' + photo + '" onerror=\"this.src="/static\/screen/wedding-default/images/default.jpg"\" /><div class="name">' + name + '</div></div>');
                    arr.push('  <div class="text"><strong><span style="margin-bottom:2px;">' + prewords +"</span><br><span style='color:red;margin-top:6px;'>"+ content + '</span></strong></div>');
                    arr.push('</div>');
                    arr.push('<div class="clear"></div>');

                    var $curBless = $(arr.join('')).appendTo($bless);
                    var bh = $bless.height();
                    if (bh >= wallHeight) {
                        $bless.animate({ top: (wallHeight - bh - 60) + 'px' }, 200);
                    }
                });
            }
        	
        	/////
        }, 'json').complete(function (res) {
            timeLoad();
        });
         
    };

    /*
    * 注册dom事件
    */
    //1.操作栏
    $('.oper-layer').on('mouseover', function () {
        $(this).clearQueue();
        $(this).animate({ 'marginLeft': '-120px' }, 200);
    }).on('mouseleave', function () {
        $(this).clearQueue();
        $(this).animate({ 'marginLeft': '-23px' }, 200);
    }).on('click', function () {
        var _this = $(this);
        //正在活动中
        if (_this.hasClass('playing')) {
            _this.find('span').html('去活动');
            _this.removeClass('playing');
            //展开活动面板
            $joinBackground.clearQueue();
            $bless.clearQueue();
            $joinBackground.animate({ 'left': '100%' }, 300);
            $bless.css('opacity', 1);
        }
        else {  //当前在祝福墙
            _this.find('span').html('祝福墙');
            _this.addClass('playing');
            $joinBackground.clearQueue();
            $bless.clearQueue();
            $joinBackground.animate({ 'left': '0' }, 300);
            $bless.css('opacity', 0);
        }
    });

    //滚动配置及运行时变量
    window.scrollParam = {
        scrollSwitch: true,             //是否继续滚动，true表示按当前速度继续滚动，false表示减速停止
        stopCount: 8,                   //按下停止后继续减速滚动的个数
        scrollCount: 0,                 //减速后已经滚动过去的个数
        speed: 50                       //滚动速度
    };
    var showCurrent = $('.pop-current');
    //始停控制按钮
    $(document).on('click', '.button', function () {
        var _this = $(this);
        //点击停止
        if (_this.hasClass('stop')) {
            scrollParam.scrollSwitch = false;
            _this.removeClass('stop');
        } else {    //点击开始
            if (scrollParam.scrollSwitch) {
                _this.addClass('stop');
                scroll();
            } else {
                alert('请等待此次滚盘结束');
            }
        }
    });
    //滚动
    var scroll = function () {
        var speed = (scrollParam.scrollSwitch ? 0 : (scrollParam.scrollCount + 1) * 13) + scrollParam.speed;
        $join.animate({ left: '-290px' }, speed, function () {
            var fc = $join.children().eq(0);
            $join.append(fc.clone()).css('left', 0);
            fc.remove();
            if (scrollParam.scrollCount < scrollParam.stopCount) {
                if (!scrollParam.scrollSwitch) {
                    scrollParam.scrollCount++;
                }
                scroll();
            } else {
                scrollParam.scrollSwitch = true;
                scrollParam.scrollCount = 0;
                var wxid = $join.children().eq(1).attr('id');
                showCurrent.html($join.children().eq(1).html()).show().animate({ 'width': '480px', height: '480px', top: '-15px' }, 500, 'easeOutBounce', function () { });
                $(document.body).bind('click', function () {
                    showCurrent.animate({ 'width': '260px', height: '260px', top: '14px' }, 500, function () {
                        $(this).hide();
                        $(this).unbind('click');
                    });
                });
            }
        });
    };

    function getQueryString(name)
    {
        var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
        if (reg.test(location.href)) return decodeURIComponent(RegExp.$2.replace(/\+/g, " ")); return undefined;
    }
});